<?php

namespace App\Abstracts\Controllers;

use Hilaoyu\Utils\UtilArr;
use Hilaoyu\Utils\UtilHtml;
use Hilaoyu\LaravelExtend\ControllerBaseResource;
use Illuminate\Http\Request;

abstract class AbstractControllerCatalogs extends ControllerBaseResource
{

    protected function alterModelsQuery($query,Request $request)
    {
        $query = parent::alterModelsQuery($query,$request);


        $group = $request->route('group','');

        if($group != ''){
            $query = $query->where('group','=',$group);
        }

        $search = [
            'keyword' => $request->input('keyword',''),
            'status' => $request->input('status',''),
        ];
        if(!empty($search['keyword'])){
            $query = $query->where(function ($query) use ($search){
                $query->orWhere('name','like','%'. $search['keyword'] .'%')
                    ->orWhere('id','like','%'. $search['keyword'] .'%');
            });
        }

        if($search['status'] != ''){
            $query = $query->where('status','=',$search['status']);

        }

        $entity = $this->entity();
        comResponse()->with( $search,'search')
            ->with( $group,'group')
            ->with( $entity->getGroup($group),'group_name')
            ->with( UtilHtml::statusOptions($search['status'],'是否启用'),'status_options');
            ;
        return $query;
    }


    protected function alterModelsLoaded($models, Request $request)
    {
        $models = parent::alterModelsLoaded($models, $request);
        $catalogs_tree = UtilArr::listToTree($models->toArray(),'','parent_id','id','children');
        return UtilArr::treeToList($catalogs_tree,'children',0);
    }

    protected function alterModelLoaded($model, Request $request)
    {
        $model = parent::alterModelLoaded($model, $request);

        if(! $group = $model->getAttribute('group')){
            $group = $request->route('group','');
        }


        $entity = $this->entity();
        $catalogs = $entity->select(function ($query) use ($group){
           return $query->where('group','=',$group)
               ->where('status','=','1');
        },['parent_id'=>'asc','name'=>'asc']);


        comResponse()
            ->with( $group,'group')
            ->with( $entity->getGroup($group),'group_name')
            ->with( UtilHtml::statusOptions(($model->status??'')),'status_options')
            ->with( $entity->buildCatalogsOptions($catalogs,($model->parent_id ?? ''),($model->id ?? ''),'_',true),'parents_options')
        ;
        return $model;
    }


    protected function alterModelSaveData(Request $request,$id='')
    {
        $data = [
            'name' => $request->input('name',''),
            'icon' => $request->input('icon',''),
            'summary' => $request->input('summary',''),
            'parent_id' => (string)$request->input('parent_id',''),
            'group' => $request->route('group',''),
            'status' => $request->input('status','0'),
        ];

        return $data;
    }







}
